<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
var jsonObj = new Object();
$(function(){
	var status = false;
	var roleName = $("#roleName").val();
	if(roleName != "ADMINISTRATOR" || roleName == null){
		status = true;
		$("#addButton").hide();
		$("#editButton").hide();
		$("#delButton").hide();
		$("#dormitoryHide").hide();
		$("#mailButton").hide();
	}
	
	$("#dialogDiv").dialog({
		title:'水电费信息维护',
		width:700,
		height:270,
		top:35,
		closed:true,
		madel:true,
		cache:false,
		onClose:function(){
			$("#dialogIframe").attr("src","");
		}
	});
	
	$("#searchButton").click(function(){
		$("#waterElectricityList").datagrid({queryParams:getParams()});
	});
	
	$("#empty").click(function(){
		$("#dormitory").textbox('setValue',"");
		$("#years").textbox('setValue',"");
		$("#month").textbox('setValue',"");
		$("#status").combobox('setValue',"");
		$("#waterElectricityList").datagrid({queryParams:getParams()});
	});
	
	$("#addButton").click(function(){
		var storiedBuilding = $("#storiedBuilding").val();
		$("#dialogIframe").attr("src","waterElectricityAddInitialize.action?storiedBuilding="+storiedBuilding);
		$("#dialogDiv").dialog('open');
	});
	
	$("#editButton").click(function(){
		var checkObj = $("input[name='id']:checked");
		if(checkObj.size()==1){
			var id = checkObj.val();
			$("#dialogIframe").attr("src","waterElectricityEditInitialize.action?id="+id);
			$("#dialogDiv").dialog('open');
		}else{
			$.messager.alert("提示","选一条记录");
		}
	});
	
	$("#delButton").click(function(){
		var ids = "";
		var checkObj = $("input[name='id']:checked");
		if(checkObj.size()==1){
			ids = checkObj.val();
		}else if(checkObj.size()>1){
			for(var i=0;i<checkObj.size();i++){
				ids+=(ids==""?"":",")+checkObj[i].value;
			}
		}else{
			$.messager.alert("提示","选择记录请");
		}
		if(ids!=""){
			$.messager.confirm({
				title:'提示',
				msg:'确定删除吗',
				top:35,
				fn:function(r){
					if(r){
						$.ajax({
							type:'POST',
							url:'waterElectricityDel.action',
							data:{id:ids},
							datatype:'json',
							success:function(msg){
								if(msg.status=="200"){
									$("#waterElectricityList").datagrid({queryParams:getParams()});
								}else{
									$.messager.alert("提示","删除失败");
								}
							}
						});
					}
				}
			});
		}
	});
	
	$("#mailButton").click(function(){
		var ids = "";
		var checkObj = $("input[name='id']:checked");
		if(checkObj.size()==1){
			ids = checkObj.val();
		}else if(checkObj.size()>1){
			for(var i;i<checkObj.size();i++){
				ids+=(ids==""?"":",")+checkObj[i].value;
			}
		}else{
			$.messager.alert("提示","选择记录请");
		}
		if(ids!=""){
			$.ajax({
				type:'POST',
				url:'waterElectricityMail.action',
				data:{id:ids},
				datatype:'json',
				success:function(msg){
					if(msg.status=="200"){
						
					}else{
						$.messager.alert("提示","邮件提醒失败");
					}
				}
			});
		}
	});
	
	$("#waterElectricityList").datagrid({
		url:'<c:url value="waterElectricityList.action" />',
		queryParams:getParams(),//pagination:true,
		toolbar:'#carCategoryToolbar',
		pagination:true,
		pagePosition:'bottom',
		pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    pageList:[10, 20, 30, 40], 
	    fitColumns:true,
	    selectOnCheck:false,
	    //sortName: 'studentId',
	    //sortOrder: 'asc',
	    remoteSort: false,
	    singleSelect:false,
	    rownumbers:true,
	    striped:true,
	    columns:[[
	    	{field:'id',title:'',width:10,align:'center',hidden:status,formatter:function(value,row,index){
	    		return '<input name="id" type="checkbox" value="'+value+'" />';
	    	}},
	    	{field:'dormitory',title:'寝室号',width:50,align:'center',sortable:true},
	    	{field:'electricity',title:'用电量',width:50,align:'center',sortable:true},
	    	{field:'electricityPrice',title:'单价（电/度）',width:70,align:'center'},
	    	{field:'water',title:'用水量',width:50,align:'center',sortable:true},
	    	{field:'waterPrice',title:'单价（水/升）',width:70,align:'center'},
	    	{field:'combined',title:'合计',width:70,align:'center'},
	    	{field:'years',title:'年份',width:40,align:'center'},
	    	{field:'month',title:'月份',width:40,align:'center'},
	    	{field:'status',title:'状态',width:40,align:'center',formatter:function(value,row,index){
	    		if(value==1)
	    			return "【未交】";
	    		else
	    			return "已交";
	    	}},
	    ]]
		/*onClickRow:function(index,row){
			$("input[type=checkbox]").val([row.id]).attr("checked",true);
	    }*/
	});
	
});

function getParams(){
	if($("#dormitory").val() != "" && $("#dormitory").val() != null){
		jsonObj.dormitory = $("#dormitory").val();
	}else{
		jsonObj.dormitory = null;
	}
	if($("#years").val() != "" && $("#years") != null){
		jsonObj.years = $("#years").val();
	}else{
		jsonObj.years = null;
	}
	if($("#month").val() != "" && $("#month").val() != null){
		jsonObj.month = $("#month").val();
	}else{
		jsonObj.month = null;
	}
	if($("#storiedBuilding").val() != "" && $("#storiedBuilding").val() != null){
		jsonObj.storiedBuilding = $("#storiedBuilding").val();
	}else{
		jsonObj.storiedBuilding = null;
	}
	if($("#status").combobox('getValue') != "" && $("#status").combobox('getValue') != null){
		jsonObj.status = $("#status").combobox('getValue');
	}else{
		jsonObj.status = null;
	}
	jsonObj.studentId = $("#studentId").val();
	jsonObj.roleName = $("#roleName").val();
	return jsonObj;
}

function closeDialog(){
	$("#dialogDiv").dialog('close');
	$("#waterElectricityList").datagrid({queryParams:getParams()});
}